package com.yz.vi.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @description:
 * @author: yutingting
 * @create: 2021-01-21 10:44
 **/
@Configuration
public class CorsConfig {
        private static final String ALL = "*";

        @Order(Ordered.HIGHEST_PRECEDENCE)
        @Bean
        public CorsWebFilter corsFilter() {
            CorsConfiguration config = new CorsConfiguration();
            // cookie跨域
            config.setAllowCredentials(Boolean.TRUE);
            config.addAllowedMethod(ALL);
            config.addAllowedOrigin(ALL);
            config.addAllowedHeader(ALL);
            // 配置前端js允许访问的自定义响应头
            config.addExposedHeader("setToken");

            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
            source.registerCorsConfiguration("/**", config);

            return new CorsWebFilter(source);
        }
}
